package com.example.xwadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.xwadmin.mapper.AdminMapper;
import com.example.xwcommon.entity.Admin;
import com.example.xwcommon.entity.Comment;
import com.example.xwcommon.entity.User;
import com.example.xwcommon.entity.Weibo;
import com.example.xwcommon.service.AdminService;
import com.example.xwcommon.service.CommentService;
import com.example.xwcommon.service.UserService;
import com.example.xwcommon.service.WeiboService;
import com.example.xwcommon.vo.Response;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController(value = "adminService")
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Qualifier("com.example.xwcommon.service.UserService")
    @Autowired
    private UserService userService;
    @Qualifier("com.example.xwcommon.service.CommentService")
    @Autowired
    private CommentService commentService;
    @Qualifier("com.example.xwcommon.service.WeiboService")
    @Autowired
    private WeiboService weiboService;

    @Override
    public Response<Admin> reg(Admin admin) {
        String hashPassword = BCrypt.hashpw(admin.getAdminLoginPwd(), BCrypt.gensalt());
        System.out.println(hashPassword);
        admin.setAdminLoginPwd(hashPassword);
        adminMapper.insert(admin);
        admin.setAdminLoginPwd(null);
        return new Response<Admin>(200,"注册成功",admin);
    }
    @Override
    public Response<Admin> login(String loginName, String loginPwd) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_loginname",loginName);
        Admin admin = adminMapper.selectOne(queryWrapper);
        if(admin == null){
            return new Response<Admin>(401,"用户名不存在",null);
        }
        if(!BCrypt.checkpw(loginPwd,admin.getAdminLoginPwd())){
            return new Response<Admin>(402,"密码错误",null);
        }
        admin.setAdminLoginPwd(null);
        List<String> pmses = adminMapper.selectPmses(admin.getAdminId());
        List<String> roles = adminMapper.selectRoles(admin.getAdminId());
        admin.setLevel(roles);
        admin.setPmses(pmses);
        return new Response<Admin>(200,"登录成功",admin);
    }

    @Override
    public Response<User> banUser(Integer userId){
        User user = userService.getUserById(userId).getData();
        return userService.updateState(user.getLoginName(),-1);
    }

    @Override
    public Response<Weibo> updateWeiboState(Integer weiboId, Integer state){
        return weiboService.updateState(weiboId,state);
    }

    @Override
    public Response<Comment> updateCommentState(Integer commentId,Integer state){
        return commentService.updateState(commentId,state);
    }
}
